/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yunbiao.communication;

import android.content.Context;
import android.content.Intent;
import android.database.Observable;
import android.util.Log;

import com.blankj.utilcode.util.ThreadUtils;

/** 
 * This class is to manage the notificatin service and to load the configuration.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public final class ServiceManager extends Observable<XmppStateObserver> {

    private Context mContext;
    private getUserNameCallback mCallback;
    private MessageHandler messageHandler;
    private boolean isConnected = false;

    private static class Holder{
        public static final ServiceManager INSTANCE = new ServiceManager();
    }

    private ServiceManager(){}

    public static ServiceManager getInstance(){
        return Holder.INSTANCE;
    }

    public  void init(Context context,MessageHandler messageHandler,getUserNameCallback callback){
        this.mContext = context;
        this.mCallback = callback;
        this.messageHandler = messageHandler;
    }

    public Context getContext(){
        return mContext;
    }

    public void setConnected(boolean connected) {
        isConnected = connected;
        for (XmppStateObserver mObserver : mObservers) {
            ThreadUtils.runOnUiThread(() -> mObserver.onConnectStateUpdate(isConnected));
        }
    }

    public boolean isConnected(){
        return isConnected;
    }

    public void handleMessage(String message) {
        if(messageHandler == null){
            return;
        }
        messageHandler.onMessage(message);
    }

    public String getUserName(){
        if(mCallback == null){
            return "";
        }
        return mCallback.getUserName();
    }

    public String getCommAddress(){
        if(mCallback == null){
            return "";
        }
        return mCallback.getCommunicationAddress();
    }

    public String getCommPort(){
        if(mCallback == null){
            return "";
        }
        return mCallback.getCommunicationPort();
    }

    public void startService() {
        Thread serviceThread = new Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent =new Intent(mContext, NotificationService.class);
                mContext.startService(intent);
            }
        });
        Log.i("xmpp info", " serviceThread.start()");
        serviceThread.start();
    }

    public void stopService() {
        Intent intent =new Intent(mContext, NotificationService.class);
        mContext.stopService(intent);
    }

    public interface getUserNameCallback{
        String getUserName();
        String getCommunicationAddress();
        String getCommunicationPort();
    }

    public interface MessageHandler{
        void onMessage(String message);
    }
}
